//系统管理-->功能管理
//by：cui 2014年9月16日18:16:30
//1.0 加载JqueryDatabase
jQuery(function($) {
    var oTable1 = $('#sample-table-2').dataTable({
        "aoColumns": [{
            "bSortable": false
        },
        {
            "bSortable": false
        },
        {
            "bSortable": false
        },
        {
            "bSortable": false
        },
        {
            "bSortable": false
        },
        {
            "bSortable": false
        },
        {
            "bSortable": false
        },
        ],
        oLanguage: {
            "sProcessing": "正在加载中......",
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "对不起，查询不到相关数据！",
            "sEmptyTable": "表中无数据存在！",
            "sInfo": "当前显示： _START_ 到 _END_ 条，共 _TOTAL_ 条",
            "sInfoFiltered": "数据表中共为 _MAX_ 条记录",
            "sSearch": "",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上一页",
                "sNext": "下一页",
                "sLast": "末页"
            }
        },
        iDisplayLength: 200,
        bLengthChange: false
    });

    $('table th input:checkbox').on('click',
    function() {
        var that = this;
        $(this).closest('table').find('tr > td:first-child input:checkbox').each(function() {
            this.checked = that.checked;
            $(this).closest('tr').toggleClass('selected');
        });
    });

    $('[data-rel="tooltip"]').tooltip({
        placement: tooltip_placement
    });
    function tooltip_placement(context, source) {
        var $source = $(source);
        var $parent = $source.closest('table'); 
        var off1 = $parent.offset();
        var w1 = $parent.width();

        var off2 = $source.offset();
        var w2 = $source.width();

        if (parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2)) return 'right';
        return 'left';
    }

})

window.onload = function() {
    // 3.0 页面初始化
    // 3.1 table表头上的样式
    $("#sample-table-2>thead tr>th").attr("style", "font-weight:bold;color:black;text-align:center;font-size:13px");

    // 3.2 table搜索样式
    $("#sample-table-2_filter label input").attr("placeholder", "搜索(多条件搜索中间用空格隔开)").attr("style", "width:50%");
    $("#sample-table-2_filter label").attr("style", "width:100%");

    $("#sample-table-2_filter").attr("style", "margin-right: -16px;");
    $("#sample-table-2_wrapper .row").first().children().eq(1).attr("style", "margin-top:-12px");
    $("#sample-table-2_wrapper").attr("style", "margin-top:-32px");
    $("#butadd").attr("style", "margin-top:-4px");

    // 3.3 增加表单行间距样式
    $("div .modal-body .row:not(#iconr)").attr("style", "margin-top:-14px;");
    $("div .modal-body .row .col-sm-3").attr("style", "margin-top:6px;text-align:right");
    $("div#myModal .modal-body .row i:lt(3)").attr("style", "margin-top:9px;text-align:right;color:gray");

    // 3.4 选择图标时样式
    $("#iconrow >div ul>li").attr("style", "cursor:pointer;");
    $("#iconrow >div ul>li").mouseover(function() {
        $(this).addClass("onIcon");
        $(this).children().addClass("onIcon");
    });
    $("#iconrow >div ul>li").mouseout(function() {
        $(this).removeClass("onIcon");
        $(this).children().removeClass("onIcon");
    });
    $("#iconrow >div ul>li").click(function() {
        $("#iconrow >div ul>li").not(this).removeClass("onClick1");
        $(this).toggleClass("onClick1");

    });

    // 3.6 按钮样式
    $("button.btn.btn-sm:gt(0)").attr("style", "margin:-5px 0px 0px 4px");

    //3.7 分页样式
    $(".dataTables_paginate.paging_bootstrap li>a i").parent().attr("style", "height:32px");
    // End 3.0
    // 4.0 表单初始化
    // 4.1 选择好图标之后点击保存按钮
    $("#saveicon").click(function() {
        var icon = $("#iconrow div ul li.onClick1").children().attr("class");
        if (icon != null) {
            $("#formIconHidden").val(icon);
            $("#iconbut").addClass(icon);
            $("#iconbut").text("  " + icon);
            $("#FunICON").val(icon);
        } else {
            $("#iconbut").removeAttr("class").attr("class", "bigger-110 no-text-shadow").text("请选择图标");
            $("#FunICON").val("");
        }
        $("#closeicon").click();
    });

    // 4.2 触发选择父节点下拉框事件
    $("#icon_but").addClass("disabled");
    $("#ParentFunID").change(function() {
        if ($("#ParentFunID").val() == "0") {
            $("#icon_but").removeClass("disabled");
        } else {
            $("#icon_but").removeClass("disabled");
            $("#icon_but").addClass("disabled");
            $("#formIconHidden").val("");
        }
    });

    // 4.3 设置checkbox
    $("#MenuTreeFlag").attr("checked", "true");

    // 4.4 初始化表单内容
    function initForm() {
        $("#ParentFunID").val("-1");
        $("#ParentFunID").removeAttr("style").removeAttr("readonly");
        $("#tpname").removeAttr("class").attr("class", "icon-lightbulb tooltip-warning cuitooltip");

        $("#FunName").val("");
        $("#FunName").removeAttr("style").removeAttr("readonly");
        $("#tname").removeAttr("class").attr("class", "icon-lightbulb tooltip-warning cuitooltip");

        $("#FunURL").val("").removeAttr("readonly");
        $("#turl").removeAttr("class").attr("class", "icon-lightbulb tooltip-warning cuitooltip");

        $("#iconbut").text("请选择图标");
        $("#iconbut").attr("class", "bigger-110 no-text-shadow").removeAttr("readonly").parent().addClass("disabled");

        $("#MenuTreeFlag").attr("checked", "true").removeAttr("onclick");

        $("#Description").val("").removeAttr("readonly");

        $("#but_saveadd").removeAttr("style");

        $("#but_savemodify").removeAttr("style").attr("style", "display:none");

    }
    // End 4.0
    // 5.4 删除操作
    $("#butdel").click(function delCheck() {
        var idString = "";
        $("table tbody input[type='checkbox']:checked").parent().parent().next().each(function() {
            idString += $(this).text() + ",";
        });
        idString = idString.substring(0, idString.length - 1);
        if (idString.length == 0) {
            $.gritter.add({
                title: '消息提醒',
                text: '请选择要删除的项',
                class_name: 'gritter-info gritter-center'
            });
        } else {
            if (!confirm("确定删除")) {
                return;
            }
            $.ajax({
                url: "admin/fun/fun_delete.action",
                dataType: "Json",
                data: {
                    idString: idString
                },
                Type: "Post",
                success: function(data) {
                    var json = eval(data);
                    if (json.result == "Success") {
                        $.gritter.add({
                            title: '消息提醒',
                            text: json.message,
                            class_name: 'gritter-info gritter-center'
                        });
                        history.go(0);
                    } else {
                        $.gritter.add({
                            title: '消息提醒',
                            text: json.message,
                            class_name: 'gritter-info gritter-center'
                        });
                    }
                }
            });
        }
    });

    // 5.0 功能
    // 5.1 刷新操作
    $("#buflush").click(function() {
        history.go(0);
    });

    // 去掉遮罩层、
    $("#bgdiv").attr("style", "display:none");

    // 5.2 初始化新增
    $("#butadd").click(function() {
        initForm();
        $("#myModal").modal();
    });

    // 5.3 新增保存
    $("#but_saveadd").click(function() {
        var ParentFunID = $("#ParentFunID").val();
        var FunName = $("#FunName").val();
        var FunURL = $("#FunURL").val();
        var FunICON = $.trim($("#icon_but").hasClass("disabled") ? "": ($("#iconbut").text() == "请选择图标" ? "": $("#iconbut").text()));
        var MenuTreeFlag = $('#MenuTreeFlag:checked').val() == 'on' ? 'true': 'false';
        var Description = $("#Description").val();

        $.ajax({
            url: "admin/fun/fun_addFun.action",
            data: $('#myModalForm').serialize(),
            dataType: "json",
            Type: "post",
            success: function(data) {
                var json = eval(data);
                if (json.result == "Success") {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                    $("#but_infoclose").click();
                    history.go(0);
                } else {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                }
            }
        });
    });

    // 5.4 删除操作
    /*
	 * $("#butdel").click(function delCheck() { var idString = ""; $("table
	 * tbody
	 * input[type='checkbox']:checked").parent().parent().next().each(function () {
	 * idString += $(this).text() + "," }); idString = idString.substring(0,
	 * idString.length - 1); if (idString.length == 0) { $.gritter.add({ title:
	 * '消息提醒', text: '请选择要删除的项', class_name: 'gritter-info gritter-center' }); }
	 * else { if (!confirm("确定删除")) { return; } $.ajax({ url: "ToDelete",
	 * dataType: "Json", data: { idString: idString }, Type: "Post", success:
	 * function (data) { var json = eval(data); if (json.result == "Success") {
	 * $.gritter.add({ title: '消息提醒', text: json.message, class_name:
	 * 'gritter-info gritter-center' }); history.go(0); } else { $.gritter.add({
	 * title: '消息提醒', text: json.message, class_name: 'gritter-info
	 * gritter-center' }); } } }); } });
	 */

    // 初始化表单
    $(".btn.btn-primary.btn-sm").mouseup(initForm());

    // 验证：添加表单
    $("#ParentFunID").blur(function() {
        var val = $(this).val();
        if (val < 0) {
            $(this).attr("style", " border-color: red;");
            $("#tpname").removeAttr("class").attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
        } else {
            $(this).removeAttr("style");
            $("#tpname").removeAttr("class").attr("class", " icon-lightbulb cuitooltip tooltip-success green");
        }
    });
    $("#ParentFunID").focus(function() {
        $(this).removeAttr("style");
        $("#tpname").removeAttr("class").attr("class", "icon-lightbulb tooltip-warning cuitooltip");
    });
    $("#FunName").blur(function() {
        var val = $(this).val();
        if (val == "") {
            $(this).attr("style", " border-color: red;");
            $("#tname").removeAttr("class").attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
        } else {
            $(this).removeAttr("style");
            $("#tname").removeAttr("class").attr("class", " icon-lightbulb cuitooltip tooltip-success green");
        }
    });
    $("#FunName").focus(function() {
        $(this).removeAttr("style");
        $("#tname").removeAttr("class").attr("class", "icon-lightbulb tooltip-warning cuitooltip");
    });
    // End 5.0
};

function initForm() {
    $("#ParentFunID").val("-1");
    $("#ParentFunID").removeAttr("style").removeAttr("readonly");
    $("#tpname").removeAttr("class").attr("class", "icon-lightbulb tooltip-warning cuitooltip");

    $("#FunName").val("");
    $("#FunName").removeAttr("style").removeAttr("readonly");
    $("#tname").removeAttr("class").attr("class", "icon-lightbulb tooltip-warning cuitooltip");

    $("#FunURL").val("").removeAttr("readonly");
    $("#turl").removeAttr("class").attr("class", "icon-lightbulb tooltip-warning cuitooltip");

    $("#iconbut").text("请选择图标");
    $("#iconbut").attr("class", "bigger-110 no-text-shadow").removeAttr("readonly").parent().addClass("disabled");

    $("#MenuTreeFlag").attr("checked", "true").removeAttr("onclick");

    $("#Description").val("").removeAttr("readonly");

    $("#but_saveadd").removeAttr("style");

    $("#but_savemodify").removeAttr("style").attr("style", "display:none");

}

// 5.5 查看详情
// $(".showInfo").click(function () {
$(".lookup11").click(function() {
    initForm();
    // 改为下拉列表多了两个parent（）
    var id = $(this).parent().parent().parent().parent().parent().children().eq(1).text();

    $.ajax({
        url: "ShowInfo",
        datatype: "json",
        type: "post",
        data: {
            id: id
        },
        success: function(data) {
            var json = eval(data);
            $("#idHidden").val(json.FunID).attr("readonly", "readonly");
            $("#FunName").val(json.FunName).attr("readonly", "readonly");
            $("#FunURL").val(json.FunURL).attr("readonly", "readonly");
            $("#ParentFunID").val(json.ParentFunID).attr("readonly", "readonly");
            $("#Description").val(json.FunDescription).attr("readonly", "readonly");
            $("#but_saveadd").attr("style", "display:none");
            var FunICON = json.FunICON.trim();
            if (FunICON != "") {
                $("#iconbut").addClass(FunICON).text(FunICON).attr("readonly", "readonly");
            }
            json.MenuTreeFlag == "True" ? $("#MenuTreeFlag").attr("checked", "on").attr("onclick", "return false") : $("#MenuTreeFlag").removeAttr("checked").attr("onclick", "return false");
        }

    });
    $("#myModal").modal();
});

// 5.6 修改详情
// $(".showModify").click(function () {
$(".modify11").click(function() {
    initForm();
    // 改为下拉列表多了两个parent（）
    var id = $(this).parent().parent().parent().parent().parent().children().eq(1).text();

    $.ajax({
        url: "admin/fun/fun_findFun.action",
        datatype: "json",
        type: "post",
        data: {
            "fun.funID": id
        },
        success: function(data) {
            var json = eval(data).Fun;
            $("#idHidden").val(json.funID);
            $("#FunName").val(json.funName);
            $("#FunURL").val(json.funUrl);
            $("#ParentFunID").val(json.parentFunID);
            $("#Description").val(json.description);
            $("#but_saveadd").attr("style", "display:none");
            $("#but_savemodify").removeAttr("style");
            var FunICON = json.funIcon.trim();
            if (FunICON != "") {
                $("#iconbut").addClass(FunICON).text(FunICON).parent().removeClass("disabled");
            }
            json.menuFlag == true ? $("#MenuTreeFlag").attr("checked", "on") : $("#MenuTreeFlag").removeAttr("checked");
        }
    });
    $("#myModal").modal();
});
// 5.6.1 修改保存
$("#but_savemodify").click(function() {
	 var pid = $("#ParentFunID").val();
	    var thisid = $("#idHidden").val();
	    if (pid == thisid) {
	        $.gritter.add({
	            title: '消息提醒',
	            text: '父节点不能选择自己',
	            class_name: 'gritter-info gritter-center'
	        });
	        return;
	    }else{
	
	
    $.ajax({
        url: "admin/fun/fun_updateFun.action",
        data: $('#myModalForm').serialize(),
        dataType: "json",
        Type: "POST",
        success: function(data) {
            var json = eval(data);
            if (json.result == "Success") {
                $.gritter.add({
                    title: '消息提醒',
                    text: json.message,
                    class_name: 'gritter-info gritter-center'
                });
                $("#but_infoclose").click();
                history.go(0);
            } else {
                $.gritter.add({
                    title: '消息提醒',
                    text: json.message,
                    class_name: 'gritter-info gritter-center'
                });
            }
        }
    });}
});

// 5.7 菜单下移
// $(".toGoDown").click(function () {
$(".movedown11").click(function() {
    initForm();
    // 改为下拉列表多了两个parent（）
    var id = $(this).parent().parent().parent().parent().parent().children().eq(1).text();
    $.ajax({
        url: "admin/fun/fun_moveDownFun.action",
        dataType: "Json",
        data: {
            "fun.funID": id
        },
        success: function(data) {
            var json = eval(data);
            if (json.result == "Success") {
                history.go(0);
            } else {
                $.gritter.add({
                    title: '消息提醒',
                    text: json.message,
                    class_name: 'gritter-info gritter-center'
                });
            }
        }
    });
});

// 5.8 菜单上移
// $(".toGoUp").click(function () {
// $(".showInfo").click(function () {
$(".moveup11").click(function() {
    initForm();
    // 改为下拉列表多了两个parent（）
    var id = $(this).parent().parent().parent().parent().parent().children().eq(1).text();
    $.ajax({
        url: "admin/fun/fun_moveUpFun.action",
        dataType: "Json",
        data: {
            "fun.funID": id
        },
        success: function(data) {
            var json = eval(data);
            if (json.result == "Success") {
                history.go(0);
            } else {
                $.gritter.add({
                    title: '消息提醒',
                    text: json.message,
                    class_name: 'gritter-info gritter-center'
                });
            }
        }
    });
});

// 5.9 菜单移动到
// $(".toGoOther").click(function () {
$(".move11").click(function() {
    initForm();
    // 改为下拉列表多了两个parent（）
    var id = $(this).parent().parent().parent().parent().parent().children().eq(1).text();
    $("#mTooltipHidden").val(id);
    $("#MName").text($(this).parent().parent().parent().children().eq(3).text());
    $("#toMoveModal").modal();
});
$("#but_msaveadd").click(function() {
    var pid = $("#MParentFunID").val();
    var thisid = $("#mTooltipHidden").val();
    if (pid == thisid) {
        $.gritter.add({
            title: '消息提醒',
            text: '不能选择自己',
            class_name: 'gritter-info gritter-center'
        });
        return;
    } else {
        $.ajax({
            url: "GoToMove",
            dataType: "Json",
            data: {
                "fun.parentFunID": pid,
                "fun.funID": thisid
            },
            type: "post",
            success: function(data) {
                var json = eval(data);
                if (json.result == "Success") {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                    $("#but_minfoclose").click();
                    history.go(0);
                } else {
                    $.gritter.add({
                        title: '消息提醒',
                        text: json.message,
                        class_name: 'gritter-info gritter-center'
                    });
                }

            }
        });
    }
});